package com.example.majiang.controller;

import com.example.majiang.dto.CommentDTO;
import com.example.majiang.dto.QuesitionDTO;
import com.example.majiang.enums.CommentTypeEnum;
import com.example.majiang.model.Question;
import com.example.majiang.service.CommentService;
import com.example.majiang.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import javax.websocket.server.PathParam;
import java.util.List;

@Controller
public class QuestionController {
    @Autowired
    CommentService commentService;
    @Autowired
    QuestionService questionService;
    @GetMapping("/question/{id}")
    public String question(@PathVariable(name="id")Integer id, Model model){

        QuesitionDTO quesitionDTO=questionService.getById(id);
        List<QuesitionDTO>relatedQuestions=questionService.selectRelated(quesitionDTO);
        List<CommentDTO> comments = commentService.listByTargetId(id, CommentTypeEnum.QUESTION);
        model.addAttribute("question",quesitionDTO);
        model.addAttribute("comments", comments);
        model.addAttribute("relatedQuestions", relatedQuestions);
        questionService.incView(id);
        return "question";
    }
}
